﻿using Arction.Wpf.BindableCharting;
using Arction.Wpf.BindableCharting.Axes;
using Arction.Wpf.BindableCharting.SeriesXY;
using Arction.Wpf.Charting.SeriesXY;
using Arction.Wpf.SignalProcessing;
using DynamicData.Kernel;
using Prism.Commands;
using ReactiveUI;
using ReactiveUI.Fody.Helpers;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Reactive;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;
using DelegateCommand = Prism.Commands.DelegateCommand;

namespace LightningChartDemo001
{
    public class MainViewModel : ReactiveObject
    {
        private double m_dLatestTemperature = 50.0;
        private Random m_random = new Random();
        public bool UseNan = false;
        
        public ObservableCollection<string> unitOptions { get; set; }

        [Reactive] public string unit { get; set; } = "位移";

        public int freq { get; set; } = 20;
        public double maxY { get; set; } = 4;

        public SignalGenerator signalGenerator {  get; set; }
        [Reactive] public AxisYCollection YAxis { get; set; }

        [Reactive] public AxisXCollection XAxis { get; set; }

        [Reactive] public SeriesPoint[] Points {  get; set; }

        public ICommand TestCommand { get;}

        public ICommand StartCommand { get; }

        public ICommand StopCommand { get; }

        public MainViewModel() 
        {
            TestCommand = new DelegateCommand(Test);
            StartCommand = new DelegateCommand(()=>signalGenerator.Start());
            StopCommand = new DelegateCommand(()=>signalGenerator.StopRequest());
            unitOptions = new ObservableCollection<string>{"速度","加速度","位移"};
            ChartInitial();
        }

        private void ChartInitial()
        {
            signalGenerator = new SignalGenerator();

            var YAxes = new AxisYCollection();
            AxisY yAxis = new AxisY();
            UpdataAxisY(ref yAxis);
            YAxes.Add(yAxis);
            YAxis = YAxes;
            YAxis[0].FormatValueLabel += UpdataUnitY;
        }

        public string UpdataUnitY(object sender, FormatValueLabelEventArgs e)
        {
            double value = e.Value;
            string label;

            switch (unit)
            {
                case "速度":
                    label = (2 * Math.PI * freq * value).ToString("0.00");
                    break;
                case "加速度":
                    label = (Math.Pow(2 * Math.PI * freq, 2) * value).ToString("0.00");
                    break;
                default:
                    label = (value * 1000).ToString();
                    break;
            }

            if (true)
            {
                Console.WriteLine("刻度线值" + value);
                Console.WriteLine("刻度线显示" + label);
            }
            return label;
        }

        public void UpdataAxisY(ref AxisY yAxis) 
        {
            
            yAxis.AxisColor = DefaultColors.SeriesForBlackBackgroundWpf[1 % DefaultColors.SeriesForBlackBackgroundWpf.Length];
            yAxis.Title.Text = "AxisY";
            yAxis.Title.Color = yAxis.AxisColor;
            yAxis.MouseScaling = true;
            yAxis.MouseScrolling = true;
            double ymax,ymin;
            yAxis.AutoDivSpacing = false;
            yAxis.MajorDivCount = 5;
            yAxis.Units.Visible = true; 
            yAxis.Units.MoveByMouse = false;
            switch (unit)
            {
                case "速度":
                    yAxis.Units.Text = " mm/s";
                    ymax = (double)(maxY / (2 * Math.PI * freq));
                    yAxis.MajorDiv = maxY / 5 / (2 * Math.PI * freq);
                    yAxis.SetRange(0, ymax);
                    break;
                case "加速度":
                    yAxis.Units.Text = " mm/s²";
                    ymax = (double)(maxY / Math.Pow(2 * Math.PI * freq, 2));
                    yAxis.MajorDiv = maxY / 5 / (Math.Pow(2 * Math.PI * freq, 2));
                    yAxis.SetRange(0, ymax);
                    break;
                default:
                    yAxis.Units.Text = " μm";
                    ymax = maxY / 1000;
                    yAxis.MajorDiv = maxY / 5 / 1000;
                    yAxis.SetRange(0, ymax);
                    break;
            }
        }

        public double CalculateTemperature()
        {
            double dY;
                dY = m_dLatestTemperature + (m_random.NextDouble() - 0.5) * 1.0;
                if (dY > 100)
                {
                    dY = 100;
                }
                if (dY < 0)
                {
                    dY = 0;
                }
                m_dLatestTemperature = dY;
            return dY;
        }

        private void Test()
        {
            Console.WriteLine("Hello World");
        }
    }
}
